//
//  LBViewController.m
//  LBScrollProgressView
//
//  Created by Liuxiaobo1991 on 06/11/2023.
//  Copyright (c) 2023 Liuxiaobo1991. All rights reserved.
//

#import "LBViewController.h"
#import <LBScrollProgressView/LBScrollProgressView.h>
#import <Masonry/Masonry.h>

@interface LBViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) LBScrollProgressViewConfig *config;

@property (nonatomic, strong) LBScrollProgressView *progressView;

@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation LBViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.progressView];
	// Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat totalWidth = scrollView.contentSize.width - CGRectGetWidth(scrollView.frame);
    CGFloat process = 0;
    if (totalWidth == 0) {
        process = 0;
    } else {
        process = scrollView.contentOffset.x / totalWidth;
    }
    self.config.process = process;
    [self.progressView updateWithModel:self.config];
}

#pragma mark - lazy load

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, CGRectGetWidth(self.view.frame), 200)];
        _scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.bounds) * 4, 0);
        _scrollView.delegate = self;
        _scrollView.backgroundColor = [UIColor purpleColor];
    }
    return _scrollView;
}

- (LBScrollProgressView *)progressView
{
    if (!_progressView) {
        _progressView = [[LBScrollProgressView alloc] initWithFrame:CGRectMake(180, 170, 30, 10)];
        [_progressView updateWithModel:self.config];
    }
    return _progressView;
}

- (LBScrollProgressViewConfig *)config
{
    if (!_config) {
        _config = [[LBScrollProgressViewConfig alloc] init];
        _config.cornerRadius = 1.5;
        _config.bgColor = [UIColor cyanColor];
        _config.indicateColor = [UIColor redColor];
        _config.totalWidth = 30;
        _config.indicateWidth = 11;
        _config.process = 0;
    }
    return _config;
}

@end
